<template>

  <el-dialog :model-value="dialogVisible" :close-on-click-modal="true" append-to-body :before-close="handleClose" :title="isAdd=='add'?'新增配置':'编辑配置'"
    width="680px">
    <el-form ref="formRef" :model="formData" :inline="false" label-width="84px" :rules="rules">
      <el-form-item label="编号" prop="code" v-if="isAdd !== 'add'">
        <el-input v-model="formData.code" placeholder="编号" :disabled="true" />
      </el-form-item>
      <el-form-item prop="invoiceType" label="发票类型" >
        <el-select v-model="formData.invoiceType" clearable placeholder="请选择发票类型" :disabled="isAdd=='edit'" style="width:100%">
          <el-option v-for="item in ticketTypeOptions" :key="item.value" :label="item.message" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="税率" prop="invoiceTax">
        <el-input-number v-model="formData.invoiceTax" :min="0" :max="100" label="税率"  class="tax-input" style="width:95%" :controls="false"></el-input-number> %
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button :loading="loading" type="primary" @click="submit()">确定</el-button>
        <el-button @click="handleClose">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script >
import api from '@/api/index'
import { statusText } from '@/util/index'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    isAdd: {
      type: String,
      default: 'add'
    },
    ticketTypeOptions: {
      type: Array,
      default:()=>[]
    }
  },

  data () {
    return {
      formData: {
        invoiceType: '',
        invoiceTypeName:'',
        invoiceTax: ''
      },
      loading: false,
         rules: {
        invoiceType:[{required: true, message: '请选择发票类型', trigger: 'blur'}]
      }

    }
  },
  methods: {
    init (data) {
      this.formData = data
    },
    handleClose () {
      this.$refs.formRef.resetFields()
      this.$emit('handleClose', false)
    },
    submit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
                this.loading = true
      const interFaceNameObj = {
        'add': 'addTax',
        'edit':'editTax'
      }
      let data = {
        ...this.formData
      }
      data.invoiceTypeName = statusText(this.ticketTypeOptions, data.invoiceType, 'message')
      console.log(data,this.isAdd)
      api.taxConfig[interFaceNameObj[this.isAdd]](data).then((res) => {
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
        this.$emit('on-submit')
        this.handleClose()
         this.loading = false
      })
        }
      })

    }

  }

}
</script>

<style lang="scss" scoped>
.dialog-footer {
  text-align: right;
}

</style>
<style lang="scss" >

  .tax-input {
    .el-input__inner {
      text-align: left;
    }
  }
  </style>
